/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ufrn.aipetro.ga.view.search;

import java.beans.PropertyChangeEvent;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import br.com.ufrn.aipetro.ga.controller.device.ValveController;
import br.com.ufrn.aipetro.ga.domain.entity.device.actuator.valve.Valve;
import br.com.ufrn.aipetro.ga.model.persist.hibernate.HibernateDaoFactory;
import br.com.ufrn.aipetro.ga.view.form.ControllerAipetro;
import br.com.ufrn.aipetro.ga.view.interfaces.View;

/**
 *
 * @author Lucas
 */
public class SearchValveFxmlController implements View<ValveController>, Initializable {

	public static final String SEARCH_BY_DEVICE_ID = "Id Interno";
	public static final String SEARCH_BY_MANUFACTURER = "Fabricante";
	public static final String SEARCH_BY_SERIAL_NUMBER = "N�mero de S�rie";
	public static final String SEARCH_BY_MAINTENANCE_DATE = "Data de Manuten��o";
	public static final String SEARCH_BY_MODEL = "Modelo";
	public static final String SEARCH_BY_TAG = "Tag";
	
	private ValveController controller;
	private Valve example;
	private List<Label> filterList;
	
	@FXML private HBox hbxFilterBar;
	@FXML private HBox hbxSearchBar;
	@FXML private ComboBox<String> cbbSearchFor;
	@FXML private TextField tfdSearchParam1;
	@FXML private TextField tfdSearchParam2;
	@FXML private Label lblSearchParamAte;
	@FXML private TableView<Valve> tblSearchResult;
	@FXML private TableColumn<Valve, String> tblSearchResultIdCol;
	@FXML private TableColumn<Valve, String> tblSearchResultModelCol;
	@FXML private TableColumn<Valve, String> tblSearchResultTagCol;
	@FXML private TableColumn<Valve, String> tblSearchResultManufacturerCol;
	@FXML private TableColumn<Valve, String> tblSearchResultSerialNumberCol;
//	public TableColumn<Controller, String> tblSearchResultSpCol;
//	public TableColumn<Controller, String> tblSearchResultKpCol;
//	public TableColumn<Controller, String> tblSearchResultTiCol;
//	public TableColumn<Controller, String> tblSearchResultTdCol;
	private ObservableList<Valve> tblSearchResultData;
    
	public SearchValveFxmlController() {
		this.example = new Valve();
		this.filterList = new ArrayList<Label>();
		initView(new ValveController(new HibernateDaoFactory()));
	}
	
	private void addParamToFilterBar() {
		Label newFilter = new Label(tfdSearchParam1.getText());
		newFilter.setStyle("-fx-border-color: gray;");
        hbxFilterBar.getChildren().add(newFilter);
        
        this.filterList.add(newFilter);
	}
	
    @Override
    public void initialize(URL url, ResourceBundle rb) {
    	configureResultTable();
    }
    
    @Override
	public void initView(ValveController controller) {
		this.controller = controller;
		this.controller.registerView(this);
	}
    
    private void configureResultTable() {
    	tblSearchResultIdCol.setCellValueFactory(new PropertyValueFactory<Valve,String>("deviceId"));
    	tblSearchResultManufacturerCol.setCellValueFactory(new PropertyValueFactory<Valve,String>("manufacturer"));
    	tblSearchResultModelCol.setCellValueFactory(new PropertyValueFactory<Valve,String>("model"));
    	tblSearchResultSerialNumberCol.setCellValueFactory(new PropertyValueFactory<Valve,String>("serialNumber"));
    	tblSearchResultTagCol.setCellValueFactory(new PropertyValueFactory<Valve,String>("tag"));
//    	tblSearchResultSpCol.setCellValueFactory(new PropertyValueFactory<Controller,String>("deviceId"));;
//    	tblSearchResultKpCol.setCellValueFactory(new PropertyValueFactory<Controller,String>("deviceId"));;
//    	tblSearchResultTiCol.setCellValueFactory(new PropertyValueFactory<Controller,String>("deviceId"));;
//    	tblSearchResultTdCol.setCellValueFactory(new PropertyValueFactory<Controller,String>("deviceId"));;
//    	tblSearchResultIdCol = new TableColumn<Controller, String>("Nome da Unidade");
//    	tblSearchResultIdCol.setCellValueFactory(new PropertyValueFactory<Controller,String>("deviceId"));
//    	tblSearchResultIdCol.setPrefWidth(350);
    	   	
//    	tblSearchResult.getColumns().clear();
//    	tblSearchResult.getColumns().add(tblSearchResultIdCol);
    	
    	tblSearchResultData = FXCollections.observableArrayList();
    	tblSearchResult.setItems(tblSearchResultData);
    }
    
    @FXML
    public void search() {
    	setSearchParam();
    	
    	List<Valve> valveList = controller.persistSearchByExample(example);
		
		tblSearchResult.getItems().clear();
		tblSearchResult.getItems().addAll(valveList);
    	
		addParamToFilterBar();
    }
    
    @FXML
    public void detail() throws IOException {
    	ValveController ctrl = new ValveController(new HibernateDaoFactory());
    	ControllerAipetro.showTab(getClass().getResource("../form/FormValve.fxml"), "Valvula", ctrl, tblSearchResult.getSelectionModel().getSelectedItem());
    }
    
    @FXML
    public void delete() {
    	controller.persistDelete(tblSearchResult.getSelectionModel().getSelectedItem());
		search();
    }

	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@FXML
	public void searchReset() {
		this.example = new Valve();
		this.tfdSearchParam1.clear();
		for (Label filter : filterList) {
			this.hbxFilterBar.getChildren().remove(filter);
		}
		this.filterList.clear();
		
		List<Valve> valveList = controller.persistSearchByExample(example);
		tblSearchResult.getItems().clear();
		tblSearchResult.getItems().addAll(valveList);
	}
	
	private void setSearchParam() {
		if (cbbSearchFor.getValue().equalsIgnoreCase(SEARCH_BY_DEVICE_ID))
			example.setDeviceId(tfdSearchParam1.getText());
		else if (cbbSearchFor.getValue().equalsIgnoreCase(SEARCH_BY_MANUFACTURER))
			example.setManufacturer(tfdSearchParam1.getText());
		else if (cbbSearchFor.getValue().equalsIgnoreCase(SEARCH_BY_SERIAL_NUMBER))
			example.setSerialNumber(tfdSearchParam1.getText());
		else if (cbbSearchFor.getValue().equalsIgnoreCase(SEARCH_BY_MODEL))
			example.setModel(tfdSearchParam1.getText());
		else if (cbbSearchFor.getValue().equalsIgnoreCase(SEARCH_BY_TAG))
			example.setTag(tfdSearchParam1.getText());
	}
	
	@FXML
	private void selectSearchFor() {
		if (cbbSearchFor.getValue().contains("Data")) {
			hbxSearchBar.getChildren().addAll(lblSearchParamAte, tfdSearchParam2);
		} else {
			hbxSearchBar.getChildren().removeAll(lblSearchParamAte, tfdSearchParam2);
		}	
	}

	@Override
	public void modelChange() {	
	}
    
}
